using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;

using My.IdlConverter.Core;

namespace My.IdlConverter.Com
{
    internal class ComTwiceProcessor : ITwiceProcessor<Interface>
    {
        public ComTwiceProcessor()
        {
        }

        public string Process(Interface interfaceObj, string orginData, MethodCollision collision)
        {
            StringBuilder sb = new StringBuilder();

            IDictionary<string,IList<Method>> collisionMethodDic = collision.CollisionMethods;

            string copy = orginData;
            IDictionary<string, IList<int>> posDic = new Dictionary<string, IList<int>>();

            foreach (KeyValuePair<string, IList<Method>> nextEntry in collisionMethodDic)
            {
                IList<int> posList = new List<int>();
                string key = "HRESULT "+nextEntry.Value[0].PureName;
                for (int i = nextEntry.Value.Count- 1; i > 0; --i)
                {
                    int index = copy.LastIndexOf(key);
                    Debug.Assert(index != -1);
                    posList.Add(index);
                    string placeHolder = key.Substring(0, key.Length - 3) + "___";
                    copy = copy.Remove(index,key.Length).Insert(index,placeHolder);
                }
                posDic.Add(key, posList);
            }

            IDictionary<string, string> placeHolderDic = new Dictionary<string, string>();
            foreach (KeyValuePair<string, IList<int>> nextEntry in posDic)
            {
                string key = nextEntry.Key;
                string placeHolder = key.Substring(0, key.Length - 1) + "_";
                IList<int> posList = nextEntry.Value;
                for (int i = 1; i <= posList.Count; ++i)
                {
                    int index = posList[i-1];
                    int no = (posList.Count-i+1);
                    placeHolder = placeHolder.Replace(placeHolder[placeHolder.Length - 1],(char)no) ;
                    orginData = orginData.Remove(index, key.Length).Insert(index,placeHolder);
                    placeHolderDic.Add(placeHolder, key + no);
                }
            }

            foreach (KeyValuePair<string, string> nextEntry in placeHolderDic)
            {
                orginData = orginData.Replace(nextEntry.Key,nextEntry.Value);
            }

            return orginData;
        }

        public string Process(string orginData)
        {
            return orginData;
        }

        //private string FindLastKey(IList<string> keys, string data)
        //{
        //    int lastIndex = -1;
        //    int pos = -1;
        //    for (int i = 0; i < keys.Count; ++i)
        //    {
        //        int index = data.LastIndexOf(keys[i]);
        //        if (index > lastIndex)
        //        {
        //            lastIndex = index;
        //            pos = i;
        //        }
        //    }
        //    Debug.Assert(lastIndex != -1);
        //    Debug.Assert(pos != -1);
        //    return keys[pos];
        //}
    }
}
